﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using control.Biz;
using System.IO;


namespace control
{
    public partial class FrmSach : Form
    {
        BizSach sach;
        public FrmSach()
        {
            InitializeComponent();

        }
        public FrmSach(BizSach _sach):this()
        {
            sach = _sach;
            displaySach();
            LoadHinh();
        }

        private void LoadHinh()
        {
            if (sach.Hinh!="")
            {

                txtHinh.Text = BizCommon.GetWebImagePath() +sach.Hinh;
                Image _img = Image.FromFile(txtHinh.Text);
                pictureBox1.Image = _img;
                pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
            }
        }

        private void displaySach()
        {
            txtMa.Text = sach.MaSach;
            txtTen.Text = sach.TenSach;
            cbbChuDe.Text = sach.TenChuDe;
            txtTacGia.Text = sach.TacGia;
            txtNamXB.Text = sach.NamXB.ToString();
            txtGia.Text = sach.GiaSach.ToString();
            dtpNgayCapNhat.Text = sach.NgayCapNhat.ToString();
            chbEnable.Checked = sach.IsEnable;
            txtMota.Text = sach.MoTa;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            int namxb=0;
            try
            {
                namxb = int.Parse(txtNamXB.Text);
            }
            catch { }
            float gia = 0;
            try
            {
                gia = float.Parse(txtGia.Text);
            }
            catch { }
            string Hinh=null;
            if (txtHinh.Text != "")
            {
                Hinh = "sach\\" + txtMa.Text;
                FileInfo fi = new FileInfo(txtHinh.Text);
                string des = BizCommon.GetWebImagePath() + Hinh;
                if(des!=txtHinh.Text)
                    try { fi.CopyTo(des); }
                    catch 
                    { 
                        
                        DateTime begintime = new DateTime(2011,11,11);
                        long ticktime = DateTime.Now.Ticks - begintime.Ticks;
                        TimeSpan _timeSpan = new TimeSpan(ticktime);
                        Hinh += _timeSpan.TotalSeconds.ToString();
                        des += _timeSpan.TotalSeconds.ToString();
                        fi.CopyTo(des);
                    }
            }
            else
            {
                Hinh = "Profile.ico";
            }
            BizChuDe _chude = (BizChuDe)cbbChuDe.SelectedItem;
            bool isEnable=new bool();
            isEnable = chbEnable.Checked==true ? true : false;
            sach = new BizSach(this.sach.IdSach, txtMa.Text, txtTen.Text, txtTacGia.Text, namxb, gia, dtpNgayCapNhat.Value, _chude, Hinh,txtMota.Text,isEnable);
            if (sach.IdSach == -1)
                sach.Insert();
            else
                sach.Update();

            this.Close();
            
        }

        private void FrmSach_Load(object sender, EventArgs e)
        {
            List<BizChuDe> chudes = new List<BizChuDe>();
            chudes = BizChuDe.GetAll();
            cbbChuDe.DataSource = chudes;
            cbbChuDe.DisplayMember = "TenChuDe";
            cbbChuDe.ValueMember = "MaChuDe";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "all file|*.*";

                dlg.Multiselect = false;
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    Image pic = Image.FromFile(dlg.FileName.ToString());
                    txtHinh.Text = dlg.FileName.ToString();
                    string[] tmp = dlg.FileNames;
                    pictureBox1.Image = pic;
                    pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
                }

            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }







    }
}
